package cn.rslee.scala.demos

/**
 * @author rslee
 *  scala的基本概念普及的类，这个类里面会演示scala的基本语法
 */
object BaseApp {
  
  //1.定义常量
  val num = 100
  //2.定义变量
  var name = "rslee"
  
  //3.定义函数(单行)
  def addOne(m : Int) = m+1
  
  //4.定义函数(多行)
  def add(a:Int,b:Int) :Int = {
    println("hello world!")
    a + b
  }
  
  //5.函数不带参数，可以不用括号调用
  def sayHello() = println("Hello")
  
  //6.函数可以通过变量引用
  val say = sayHello
  
  //7.创建匿名函数
  val myFunc = (x : Int) => x +1
  
  //8.curry化(柯里化？库里化？)函数。简单来说就是可以延时传参数
  
  def mutliply(m : Int)(n : Int) : Int = m * n
  
  //你可以直接传进去两个参数
  mutliply(3)(5)
  //也可以先传一个参数，然后再传另外一个
  
  val timesTwo = mutliply(3) _
  
  timesTwo(5)
  
  //你可以对任意多参数的函数执行柯里化，使用curried方法即可
  def adder(a : Int,b:Int)= a+b
  
  (adder _).curried
  
  //9.可变长度参数
  
  def capitalizeAll(args : String*){
    args.map { arg => arg.capitalize }
  }
  
  capitalizeAll("hello","world")
  
  //10.普通类定义
  class Calculator{
    
    val brand : String = "SF"
    def add(m : Int,n : Int) : Int = m + n
    
  }
  
  def c = new Calculator
  
  c.brand
  
  c.add(100, 30)
  
  // 11.带参数的构造类定义，类似java里面的构造函数
  
  class Sample(name : String){
    val color : String = if(name == "IT"){
      "BLUE"
    } else if (name == "SF"){
      "BLACK"
    }else{
      "WHITE"
    }
    
    def hello = println(name)
    
    
  }
  
  // 12.继承
  
  class SaySample(name:String) extends Sample(name:String){
    
    //重写
    override def hello = println("hello "+name)
    
    //重载
    def hello(word : String) = println("你好 "+word)
    
    
  }
  //13.抽象类
  abstract class Shape{
    def say(name : String) = println(name)
    //不定义函数体，实现由子类实现
    def getArea():Int
  }
  
  class Circle(r : Int) extends Shape{
    def getArea():Int = r* r*3
  }
  
  //14.scala版的aop切面:Traits（treit）
  //Traits可以将新的方法附加到已有的对象上面，扩展旧对象的方法
  
  trait Demo{
    val dream:String
    def dreaming() = println("I dream a "+dream)
  }
 
  class MyCircle(r : Int) extends Circle(r : Int) with Demo{
    val dream = "star"
  }
  
  //单例对象
  
  object Timer{
    var count = 0
    
    def currentCount(): Long = {
      count +=1
      count
    }
  }
  
  
  def main(args : Array[String]){
    val s = new SaySample("rslee")
    s.hello("world")
    s.hello
    
    val c = new Circle(10)
    println(c.getArea())
    
    val m = new MyCircle(5)
    m.dreaming()
    
    Timer.currentCount()
    Timer.currentCount()
  }
  
  
}